//
//  UIView+viewExtension.m
//  FCTools
//
//  Created by WJC on 2018/10/26.
//  Copyright © 2018 wjc. All rights reserved.
//

#import "UIView+viewExtension.h"
#import <objc/runtime.h>

@implementation UIView (viewExtension)
#define WJCScreenH [UIScreen mainScreen].bounds.size.height
#define WJCScreenW [UIScreen mainScreen].bounds.size.width
+ (instancetype)viewFromXibWithBundle:(NSBundle *)bundle
{
    return [[bundle loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
}
//+(instancetype)getXibWithBundle:(NSBundle *)bundle AndController:(UIViewController *)controller{
//    WJCLog(@"1");
//    return [[bundle loadNibNamed:NSStringFromClass(controller) owner:nil options:nil] lastObject];
//}
+ (instancetype)viewFromXib{
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
}

-(void)setTxtTag:(NSString *)txtTag{
     return objc_setAssociatedObject(self,@selector(txtTag),txtTag,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(NSString *)txtTag{
     return objc_getAssociatedObject(self, _cmd);
}
//-(void)setButtonTitle:(NSString *)buttonTitle{
//    return objc_setAssociatedObject(self,@selector(buttonTitle),buttonTitle,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
//}
//-(NSString *)buttonTitle{
//    return objc_getAssociatedObject(self, _cmd);
//}
//顶部所有圆角
- (void)drawTopCornerRadius:(NSInteger)radius {
    // 任意圆角
    CGPathRef path = [UIBezierPath bezierPathWithRoundedRect:self.bounds
    byRoundingCorners:UIRectCornerTopRight|UIRectCornerTopLeft cornerRadii:CGSizeMake(radius, radius)].CGPath;
    CAShapeLayer *lay = [CAShapeLayer layer];
    lay.path = path;
    self.layer.mask = lay;
}

// 顶部所有圆角
- (void)drawBottomCornerRadius:(NSInteger)radius {
    // 任意圆角
    CGPathRef path = [UIBezierPath bezierPathWithRoundedRect:self.bounds
    byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadii:CGSizeMake(radius, radius)].CGPath;
    CAShapeLayer *lay = [CAShapeLayer layer];
    lay.path = path;
    self.layer.mask = lay;
}

-(void)draw6Corner{
      float viewWidth = self.width;
            UIBezierPath * path = [UIBezierPath bezierPath];
            path.lineWidth = 1;
            [[UIColor whiteColor] setStroke];
            [path moveToPoint:CGPointMake(self.height*0.25, 0)];
            [path addLineToPoint:CGPointMake(0, self.height*0.5)];
            [path addLineToPoint:CGPointMake(self.height*0.25, self.height)];
            [path addLineToPoint:CGPointMake(viewWidth-(self.height*0.25), self.height)];
            [path addLineToPoint:CGPointMake(viewWidth, self.height*0.5)];
            [path addLineToPoint:CGPointMake(viewWidth-(self.height*0.25), 0)];
            [path closePath];
            
            CAShapeLayer * shapLayer = [CAShapeLayer layer];
            shapLayer.lineWidth = 1;
            shapLayer.strokeColor = [UIColor whiteColor].CGColor;
            shapLayer.path = path.CGPath;
            self.layer.mask = shapLayer;
}
- (void)setX:(CGFloat)x
{
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}

- (CGFloat)x
{
    return self.frame.origin.x;
}
-(void)setRadius:(CGFloat)radius{
    self.layer.cornerRadius = radius;
    
}


-(CGFloat)radius{
    return self.layer.cornerRadius;
}
-(void)setBorderWidth:(CGFloat)borderWidth{
    self.layer.borderWidth = borderWidth;
}
-(CGFloat)borderWidth{
    return self.layer.borderWidth;
}
-(void)setMasksToBounds:(NSInteger)masksToBounds{
    self.layer.masksToBounds = masksToBounds>0?YES:NO;
}
-(NSInteger)masksToBounds{
    return self.layer.masksToBounds?1:0;;
}
-(void)setBorderColor:(UIColor *)borderColor{
    self.layer.borderColor = borderColor.CGColor;
}
-(UIColor *)borderColor{
    return [[UIColor alloc] initWithCGColor:self.layer.borderColor];
}
- (void)setY:(CGFloat)y
{
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}

- (CGFloat)y
{
    return self.frame.origin.y;
}

- (void)setWidth:(CGFloat)width
{
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}

- (CGFloat)width
{
    return self.frame.size.width;
}

- (void)setHeight:(CGFloat)height
{
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}

- (CGFloat)height
{
    return self.frame.size.height;
}

- (void)setCenterX:(CGFloat)centerX
{
    CGPoint center = self.center;
    center.x = centerX;
    self.center = center;
}

- (CGFloat)centerX
{
    return self.center.x;
}

- (void)setCenterY:(CGFloat)centerY
{
    CGPoint center = self.center;
    center.y = centerY;
    self.center = center;
}

- (CGFloat)centerY
{
    return self.center.y;
}

- (CGFloat)maxX {
    return self.frame.origin.x + self.frame.size.width;
}

- (void)setMaxX:(CGFloat)right {
    CGRect frame = self.frame;
    frame.origin.x = right - frame.size.width;
    self.frame = frame;
}

- (CGFloat)maxY {
    return self.frame.origin.y + self.frame.size.height;
}

- (void)setMaxY:(CGFloat)bottom {
    CGRect frame = self.frame;
    frame.origin.y = bottom - frame.size.height;
    self.frame = frame;
}

- (CGPoint)origin {
    return self.frame.origin;
}

- (void)setOrigin:(CGPoint)origin {
    CGRect frame = self.frame;
    frame.origin = origin;
    self.frame = frame;
}

- (CGSize)size {
    return self.frame.size;
}

- (void)setSize:(CGSize)size {
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
}
- (CGFloat)bottom
{
    return self.frame.origin.y + self.frame.size.height;
}

- (void)setBottom:(CGFloat)bottom
{
    CGRect newFrame   = self.frame;
    newFrame.origin.y = bottom - self.frame.size.height;
    self.frame        = newFrame;
}

- (CGFloat)left
{
    return self.frame.origin.x;
}

- (void)setLeft:(CGFloat)left
{
    CGRect newFrame   = self.frame;
    newFrame.origin.x = left;
    self.frame        = newFrame;
}

- (CGFloat)right
{
    return self.frame.origin.x + self.frame.size.width;
}

- (void)setRight:(CGFloat)right
{
    CGRect newFrame   = self.frame;
    newFrame.origin.x = right - self.frame.size.width;
    self.frame        = newFrame;
}

    //获取该视图的控制器
- (UIViewController*)viewController{
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController*)nextResponder;
        }
    }
    return nil;
}

    //删除当前视图内的所有子视图
- (void) removeChildViews{
    for (UIView *cv in self.subviews) {
        [cv removeFromSuperview];
    }
}

    //删除tableview底部多余横线
- (void)setExtraCellLineHidden:(UITableView *)tableView
{
    
    UIView *view = [UIView new];
    
    view.backgroundColor = [UIColor clearColor];
    
    [tableView setTableFooterView:view];
}

/** 设置控件圆角 */
- (void)byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii
{
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:cornerRadii];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
}

- (CGRect)getFrameWithSuperView:(UIView *)superView{
    CGRect frame = [self.superview convertRect:self.frame toView:superView];
    return frame;
}
+ (UIView*)getTopCercleWithPorint:(CGPoint)point{
    UIView *coverView=[[UIView alloc]initWithFrame:CGRectMake(point.x, point.y, WJCScreenW,10 )];
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(WJCScreenW*-2, 0, WJCScreenW*5, WJCScreenW*5) byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(WJCScreenW*7, WJCScreenW*7)];
    coverView.backgroundColor=[UIColor whiteColor];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = coverView.bounds;
    maskLayer.path = maskPath.CGPath;
    coverView.layer.mask = maskLayer;
    return coverView;
}
- (UIView*)subViewOfClassName:(NSString*)className {
    for (UIView* subView in self.subviews) {
        if ([NSStringFromClass(subView.class) isEqualToString:className]) {
            return subView;
        }
        
        UIView* resultFound = [subView subViewOfClassName:className];
        if (resultFound) {
            return resultFound;
        }
    }
    return nil;
}

/**
 ** lineView:       需要绘制成虚线的view
 ** lineLength:     虚线的宽度
 ** lineSpacing:    虚线的间距
 ** lineColor:      虚线的颜色
 **/
- (void)drawDashLine:(UIView *)lineView lineLength:(int)lineLength lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor
{
        //设置虚线边框
    CAShapeLayer *borderLayer = [CAShapeLayer layer];
    borderLayer.bounds = CGRectMake(0, 0, lineView.width + 10, lineView.height + 10);
        //中心点位置
    borderLayer.position = CGPointMake(lineView.width *0.5, lineView.height *0.5);
    
    borderLayer.path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, lineView.width + 10, lineView.height + 10)].CGPath;
        //边框的宽度
    borderLayer.lineWidth = 1;
        //边框虚线线段的宽度
    borderLayer.lineDashPattern = @[@(lineLength),@(lineSpacing)];
    borderLayer.fillColor = [UIColor clearColor].CGColor;
    borderLayer.strokeColor = lineColor.CGColor;
    [lineView.layer addSublayer:borderLayer];
}

#pragma mark - 渐变背景色
+ (UIView *)az_gradientViewWithColors:(NSArray<UIColor *> *)colors locations:(NSArray<NSNumber *> *)locations startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint {
    UIView *view = [[self alloc] init];
    [view az_setGradientBackgroundWithColors:colors locations:locations startPoint:startPoint endPoint:endPoint];
    return view;
}

- (void)az_setGradientBackgroundWithColors:(NSArray<UIColor *> *)colors locations:(NSArray<NSNumber *> *)locations startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint {
    NSMutableArray *colorsM = [NSMutableArray array];
    for (UIColor *color in colors) {
        [colorsM addObject:(__bridge id)color.CGColor];
    }
    self.az_colors = [colorsM copy];
    self.az_locations = locations;
    self.az_startPoint = startPoint;
    self.az_endPoint = endPoint;
}

#pragma mark- Getter&Setter

- (NSArray *)az_colors {
    return objc_getAssociatedObject(self, _cmd);
}

- (void)setAz_colors:(NSArray *)colors {
    objc_setAssociatedObject(self, @selector(az_colors), colors, OBJC_ASSOCIATION_COPY_NONATOMIC);
    if ([self.layer isKindOfClass:[CAGradientLayer class]]) {
        [((CAGradientLayer *)self.layer) setColors:self.az_colors];
    }
}

- (NSArray<NSNumber *> *)az_locations {
    return objc_getAssociatedObject(self, _cmd);
}

- (void)setAz_locations:(NSArray<NSNumber *> *)locations {
    objc_setAssociatedObject(self, @selector(az_locations), locations, OBJC_ASSOCIATION_COPY_NONATOMIC);
    if ([self.layer isKindOfClass:[CAGradientLayer class]]) {
        [((CAGradientLayer *)self.layer) setLocations:self.az_locations];
    }
}

- (CGPoint)az_startPoint {
    return [objc_getAssociatedObject(self, _cmd) CGPointValue];
}

- (void)setAz_startPoint:(CGPoint)startPoint {
    objc_setAssociatedObject(self, @selector(az_startPoint), [NSValue valueWithCGPoint:startPoint], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    if ([self.layer isKindOfClass:[CAGradientLayer class]]) {
        [((CAGradientLayer *)self.layer) setStartPoint:self.az_startPoint];
    }
}

- (CGPoint)az_endPoint {
    return [objc_getAssociatedObject(self, _cmd) CGPointValue];
}

- (void)setAz_endPoint:(CGPoint)endPoint {
    objc_setAssociatedObject(self, @selector(az_endPoint), [NSValue valueWithCGPoint:endPoint], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    if ([self.layer isKindOfClass:[CAGradientLayer class]]) {
        [((CAGradientLayer *)self.layer) setEndPoint:self.az_endPoint];
    }
}
#pragma mark - 渐变背景色 end
//将当前视图内空间改成不可编辑
-(void)setEditWithView:(bool)edit{
    if(!edit){
        for (UIView *v in self.subviews) {
            if (v.subviews.count>0) {
                [v setEditWithView:edit];
            }
            if ([v isKindOfClass:[UITextField class]]||[v isKindOfClass:[UIButton class]]||[v isKindOfClass:[UITextView class]]) {
                v.userInteractionEnabled = NO;
            }
        }
    }else{
        for (UIView *v in self.subviews) {
            if (v.subviews.count>0) {
                [v setEditWithView:edit];
            }
            if ([v isKindOfClass:[UITextField class]]||[v isKindOfClass:[UIButton class]]||[v isKindOfClass:[UITextView class]]) {
                v.userInteractionEnabled = YES;
            }
        }
    }
}
@end
